package org.huanglei.account.view;

import java.util.Map;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.huanglei.account.MyApplication;
import org.huanglei.account.R;
import org.huanglei.account.provider.DataProvider;
import org.huanglei.account.provider.DataProvider.StaticDateFormat;
import org.huanglei.account.provider.DataProviderFactory;
import org.huanglei.account.view.base.TitleBarActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

/**
 * 财务报表
 * @author 黄磊
 *
 */
public class AccountReportActivity extends TitleBarActivity {
	
	private final Context context = this;
	
	/**
	 * 曲线图数据
	 */
	private Map<String, Double> staticDate = null;
	
	/**
	 * 饼图数据
	 */
	private Map<String, Double> pieChartDate = null;
	
	private TabHost tabHost = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		MyApplication.getInstance().addActivity(this);
		
		setContentView(R.layout.account_chart_tab);
		initData();
		tabHost = (TabHost) findViewById(R.id.tabhost_chart);
		tabHost.setup();
		
		initPieChartView();
		initGraphView();
		
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(getString(R.string.chart_bing)).setContent(R.id.tab1));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(getString(R.string.chart_quxian)).setContent(R.id.tab2));
		// 隐藏标签
		tabHost.getTabWidget().setVisibility(View.GONE);
		
		//处理tabId
	    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String tabId) {
                     Log.i("HelloTabWidget", tabId) ;
			}
		});
		  
	}
	
	/**
	 * 初始化报表数据
	 */
	private void initData() {
		DataProvider dataProvider = DataProviderFactory.getProvider(context,
				DataProviderFactory.LOCAL);
		
		// 获得曲线图数据
		staticDate = dataProvider.getDateStatic("", StaticDateFormat.MONTH);
		for (Map.Entry<String, Double> entry : staticDate.entrySet()) {
			Log.i("map", entry.getKey() + "-" + entry.getValue());
		}
		
		// 获得饼图数据
		pieChartDate = dataProvider.getStatistics(null, StaticDateFormat.ALL);
	}
	
	/**
	 * @return 饼图
	 */
	private LinearLayout  initPieChartView(){
	    int[] colors = new int[] {Color.LTGRAY, Color.GREEN, Color.MAGENTA, Color.GRAY, Color.CYAN, Color.RED};
	    DefaultRenderer renderer = new DefaultRenderer();
	    
		if (pieChartDate.isEmpty()) {
			// 无数据
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(Color.GREEN);
			renderer.addSeriesRenderer(r);
		} else {
			// 有数据
			for (int color : colors) {
				SimpleSeriesRenderer r = new SimpleSeriesRenderer();
				r.setColor(color);
				renderer.addSeriesRenderer(r);
			}
		}
	    
	    renderer.setLabelsTextSize(10);
	    
	    GraphicalView graphicalView =  ChartFactory.getPieChartView(context, buildCategoryDataset("A"), renderer);
		LinearLayout layout = (LinearLayout)findViewById(R.id.tab1);
		
		// tabLayout start
		LinearLayout tabLayout = new LinearLayout(context);
		tabLayout.setOrientation(LinearLayout.VERTICAL);
		
		// 报表
		LinearLayout chartLayout = new LinearLayout(context);
		chartLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT,1
            ));
		chartLayout.addView(graphicalView);
		tabLayout.addView(chartLayout);
		
		// 图片按钮
		LinearLayout controlLayout = (LinearLayout) LayoutInflater
				.from(context).inflate(R.layout.chart_tab_control, null);
		ImageView pic2 = (ImageView) controlLayout
				.findViewById(R.id.pic_chart2);
		// 监听图片单击事件
		pic2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				tabHost.setCurrentTab(1);
			}
		});
		tabLayout.addView(controlLayout);
		
		layout.addView(tabLayout);
		return layout;
	}
	
	/**
	 * @return 曲线图
	 */
	private LinearLayout  initGraphView(){
		XYMultipleSeriesRenderer xyMultipleSeriesRenderer = new XYMultipleSeriesRenderer();
		XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();

		CategorySeries series = new CategorySeries(getResources().getString(R.string.chart_year_month_income));
		
		for(int i = 1; i < 13; i++){
			Double money = staticDate.get("" + i);
			money = money == null ? 0 : money;
			series.add("" + i, money);
		}

		XYSeriesRenderer seriesRenderer = new XYSeriesRenderer();
		seriesRenderer.setColor(Color.RED);
		seriesRenderer.setLineWidth(2);
		seriesRenderer.setPointStyle(PointStyle.CIRCLE);

		xyMultipleSeriesRenderer.addSeriesRenderer(seriesRenderer);
		// 轴颜色
		xyMultipleSeriesRenderer.setAxesColor(Color.BLACK);
		xyMultipleSeriesRenderer.setShowGrid(true);
		// 边缘颜色
		xyMultipleSeriesRenderer.setMarginsColor(Color.blue(1));
		xyMultipleSeriesRenderer.setBarSpacing(0.5);
		
		// x，y坐标标题颜色
		xyMultipleSeriesRenderer.setXLabelsColor(Color.BLACK);
		xyMultipleSeriesRenderer.setYLabelsColor(0, Color.BLACK);
		
		// x,y坐标轴
		xyMultipleSeriesRenderer.setXLabels(12);
		xyMultipleSeriesRenderer.setYLabels(10);
		
		// 标题颜色
		xyMultipleSeriesRenderer.setLabelsColor(Color.BLACK);
		xyMultipleSeriesRenderer.setChartTitle(getString(R.string.chart_xiaofei_baobiao));
		xyMultipleSeriesRenderer.setXTitle(getString(R.string.chart_yuefen));
		xyMultipleSeriesRenderer.setYTitle(getString(R.string.chart_jine));
		
		xyMultipleSeriesDataset.addSeries(series.toXYSeries());

		GraphicalView graphicalView = ChartFactory.getLineChartView(context,
				xyMultipleSeriesDataset, xyMultipleSeriesRenderer);

		LinearLayout layout = (LinearLayout)findViewById(R.id.tab2);
		
		// tabLayout start
		LinearLayout tabLayout = new LinearLayout(context);
		tabLayout.setOrientation(LinearLayout.VERTICAL);

		// 报表
		LinearLayout chartLayout = new LinearLayout(context);
		chartLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT,1
            ));
		chartLayout.addView(graphicalView);
		tabLayout.addView(chartLayout);
		
		// 图片按钮
		LinearLayout controlLayout = (LinearLayout) LayoutInflater
				.from(context).inflate(R.layout.chart_tab_control, null);
		ImageView pic1 = (ImageView) controlLayout
				.findViewById(R.id.pic_chart1);
		// 监听图片单击事件
		pic1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				tabHost.setCurrentTab(0);
			}
		});
		tabLayout.addView(controlLayout);
		
		layout.addView(tabLayout);
		return layout;
	}
	

	/**
	 * Builds a category series using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @return the category series
	 */
	private CategorySeries buildCategoryDataset(String title) {
		CategorySeries series = new CategorySeries(title);
		
		if(pieChartDate.isEmpty()){
			series.add(getString(R.string.chart_no_data), 1);
			return series;
		}
		
		String[] accountTypes = getResources().getStringArray(R.array.account_types);
		for(String key : accountTypes){
			series.add(key, getPieChartValue(key));
		}
		
		pieChartDate = null;
		
		return series;
	}
	
	private double getPieChartValue(String key){
		if(pieChartDate.isEmpty()){
			return 0;
		}
		
		Double value = pieChartDate.get(key);
		if(value != null){
			return value;
		}
		
		return 0;
	}

	@Override
	protected void init() {
		initTitleBar();
		initData();
	}

	@Override
	protected int getContentView() {
		return R.layout.account_report;
	}
	
	private void initTitleBar() {
		setBarTitle(getResources().getString(R.string.account_report));
		showBarView(TitleBarView.TBV_BACK, TitleBarActivity.CL_BACK);
		showBarView(TitleBarView.TBV_HOME,
				CL_HOME.setObject(DashboardActivity.class));
	}


}